//turret.txt - Simple script for turrets
// Cell 0 - ability
//Cell 2,3 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.

begincreaturescript;

short i,target;
short last_abil;
short ae_range = 8;
short sizzle = 0;

body;

beginstate INIT_STATE;
	last_abil = get_current_tick();
	
	set_level(ME,28);
	change_max_health(ME,200);
	set_boss_level(ME,1);
	
	if (gf(get_memory_cell(2),get_memory_cell(3)) > 0)
		erase_char(ME);
	break;

beginstate DEAD_STATE;
	set_flag(get_memory_cell(2),get_memory_cell(3),1);
break;

beginstate START_STATE; 
	if (who_shot_me() >= 0) {

		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}


		
	if (get_foe_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking

	if (target_ok() == FALSE)
		set_state(START_STATE);
	if (dist_to_char(get_target()) > 8) {
		set_foe_target(ME,-1);
		set_state(START_STATE);
		}

	// call out guys to help
	if (get_attitude(ME) >= 10)
		sf(52,16 + get_memory_cell(0),1);
	
	if ((get_attitude(ME) >= 10) && (is_combat()) && 
	  (tick_difference(last_abil,get_current_tick()) > 0)) {
		run_char_animation(2,1,35);	

	  	if (get_memory_cell(0) == 0) { // energy
		  	place_particle_num(ME,1,4,8);
			print_named_str(ME,"radiates a cloud of searing energy.");
			pc_heard_sound_delay(179,250);						
	  		create_missile_spiral(155,40,ae_range,2);
	  		damage_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,10),ae_range,1,0);
			status_nearby(3,ae_range,20,0);
	  		}

	  	if (get_memory_cell(0) == 1) { // debuff
		  	place_particle_num(ME,3,6,8);
			spray_missiles(43,8,ae_range);
			print_named_str(ME,"radiates an aura of static.");
			pc_heard_sound_delay(201,250);						
			particle_nearby(ae_range,8,2,4,2);
			status_nearby(-6,ae_range,1,0);
			status_nearby(-10,ae_range,8,0);
			status_nearby(-10,ae_range,9,0);
			status_nearby(-10,ae_range,11,0);
			status_nearby(-10,ae_range,12,0);
			status_nearby(-10,ae_range,13,0);
			status_nearby(-10,ae_range,14,0);
			status_nearby(-10,ae_range,15,0);
			status_nearby(-10,ae_range,16,0);
			status_nearby(-10,ae_range,18,0);
			status_nearby(-10,ae_range,19,0);
	  		}
	  	if (get_memory_cell(0) == 2) { // debuff
		  	place_particle_num(ME,4,6,8);
			print_named_str(ME,"emits some distracting vibrations.");
			pc_heard_sound_delay(107,250);						
			status_nearby(get_level(ME) + 5 + get_attack_bonus(ME),ae_range,7,0);
			status_nearby(-6,ae_range,0,0);
			status_nearby(-6,ae_range,8,0);
	  		}

		last_abil = get_current_tick();
		end();
		}
		

	do_attack();

	turret_heal();
break;

beginstate TALKING_STATE;
	if (gf(52,7) > 0)
		print_str_color("The servant mind is too distracted to talk to you.",1);
		else begin_talk_mode(30 + 20 * get_memory_cell(0));
	break;